import { EnumSchema } from '@/utils/common/types';
import { PresetColorEnum } from '@/enums/colorEnum';
import { useI18n } from '@/hooks/web/useI18n';

/** 字节序格式(大小端类型)枚举
 * https://cloud.tencent.com/developer/article/1601823
 */
export enum EndianFormatEnum {
  /** 无 */
  None = 0,
  /** 大端序(ABCD) */
  BigEndian = 1,
  /** 大端字节交换(BADC) */
  BigEndianByteSwap = 2,
  /** 小端序(DCBA) */
  LittleEndian = 3,
  /** 小端字节交换(CDAB) */
  LittleEndianByteSwap = 4,
}

/** 字节序格式实体 */
export class EndianFormatEntity {
  /** 字节序格式 */
  EndianFormat: EndianFormatEnum;
  /** 显示描述 */
  Display: string;
  /** 颜色 */
  Color?: PresetColorEnum | string;

  constructor(endianFormat: EndianFormatEnum, color?: PresetColorEnum | string) {
    const { t } = useI18n();
    this.EndianFormat = endianFormat;
    this.Color = color;
    switch (endianFormat) {
      case EndianFormatEnum.None:
        // this.Display = '无';
        this.Display = t('iot.enum.endianFormat.none');
        this.Color = this.Color ? this.Color : PresetColorEnum.Default;
        break;
      case EndianFormatEnum.BigEndian:
        // this.Display = '大端序(ABCD)';
        this.Display = t('iot.enum.endianFormat.bigEndian');
        this.Color = this.Color ? this.Color : PresetColorEnum.Blue;
        break;
      case EndianFormatEnum.BigEndianByteSwap:
        // this.Display = '大端字节交换(BADC)';
        this.Display = t('iot.enum.endianFormat.bigEndianByteSwap');
        this.Color = this.Color ? this.Color : PresetColorEnum.Geekblue;
        break;
      case EndianFormatEnum.LittleEndian:
        // this.Display = '小端序(DCBA)';
        this.Display = t('iot.enum.endianFormat.littleEndian');
        this.Color = this.Color ? this.Color : PresetColorEnum.Green;
        break;
      case EndianFormatEnum.LittleEndianByteSwap:
        // this.Display = '小端字节交换(CDAB)';
        this.Display = t('iot.enum.endianFormat.littleEndianByteSwap');
        this.Color = this.Color ? this.Color : PresetColorEnum.Cyan;
        break;
    }
  }
}

//#region 获取字节序格式枚举架构
/**
 * 获取字节序格式枚举架构
 */
export function getEndianFormatEnumSchema(enableTag = true): EnumSchema[] {
  const enumSchema: EnumSchema[] = [];
  const entities: EndianFormatEntity[] = [
    new EndianFormatEntity(EndianFormatEnum.None),
    new EndianFormatEntity(EndianFormatEnum.BigEndian),
    new EndianFormatEntity(EndianFormatEnum.BigEndianByteSwap),
    new EndianFormatEntity(EndianFormatEnum.LittleEndian),
    new EndianFormatEntity(EndianFormatEnum.LittleEndianByteSwap),
  ];
  entities.forEach((item) => {
    enumSchema.push({
      key: item.EndianFormat as number,
      label: item.Display,
      value: item.EndianFormat as number,
      tag: enableTag,
      tagColor: item.Color,
    });
  });
  return enumSchema;
}
//#endregion
